// pages/Main/mainIndex/mainIndex.js
var common = require('../../../utils/common.js')
var businessLogic = require('../../../utils/NetWork/Model/MainbusinessLogic.js')
let asign = 0;
let bbb = [];
let aaa = [];
let ccc = [];
let ddd = [];
let bsign = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    subject: "知识点：",
    sub: "古文赏析",
    title: "三年级语文测试",
    time: "订正",
    answernum: "答题次数：",
    red: "3",
    content: [],
    num: 1,
    scrollHeight: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.id)
    let c = common.Getorgid();
    var scrollHeight = scrollHeight;
    wx.getSystemInfo({
      success: function (res) {
        scrollHeight = res.windowHeight
      }
    });
    if (c.role == "学生") {
      this.setData({
        orgid: c.orgid,
        userid: c.userid,
        scrollHeight: scrollHeight,
      });
    }
    else {
      this.setData({
        orgid: c.orgid,
        userid: options.id,
        scrollHeight: scrollHeight,
      });
    }
    this.misTopic();
  },
  //8.11.1获取试卷内容信息及相关知识点
  misTopic: function () {
    bsign = 0;
    common.showLoading("加载中", "loading");
    let params = { "page": this.data.num, "lines": 10, "orgid": this.data.orgid, "userid": this.data.userid, "searchField": "peopleid,type", "searchValue": "" + this.data.userid + ",0" }
    businessLogic.GetThePapers(params, (res) => {

      for (let a = 0; a < res.infos.length; a++) {
        // console.log(res[a].title);
        var x = "";
        var kemucolor1 = ""
        switch (res.infos[a].subject_dictionary_title) {
          case "语文":
            x = "yuwen"
            break;
          case "数学":
            x = "shuxue"
            break;
          case "英语":
            x = "english"
            break;
          case "物理":
            x = "wuli"
            break;
          case "化学":
            x = "huaxue"
            break;
          case "政治":
            x = "zhengzhi"
            break;

          default:
            x = "zhengzhi";
        }
        res.infos[a].color = [];
        res.infos[a].color.push(x);
      }
      var newcontent = this.data.content;
      for (var a = 0; a < res.infos.length; a++) {
        newcontent.push(res.infos[a]);
      }
      //console.log(res)
      this.setData({
        content: newcontent,
        lenpage: res.page.pages
      })
      for (var i = aaa.length; i < this.data.content.length; i++) {
        aaa[aaa.length] = this.data.content[i];
        if (i == (this.data.content.length - 1)) {
          this.getdata(aaa.length, this.data, res.infos)
        }
      }
    })
  },
  getdata: function (alength, data, res) {
    if (asign == alength) {
      asign = alength;
      bsign = 0;
    } else {
      //8.11.2 获取该人每道题的错题数
      let paramsm = { "classid": res[bsign].id, "orgid": data.orgid, "userid": data.userid, "searchField": "statemark", "searchValue": "0" }
      businessLogic.GetMistakeNum(paramsm, (resa) => {

        bbb.push(resa.length);
        for (var k = 0; k < res.length; k++) {
          res[k].len = bbb[k];
        }
        if (asign == (alength - 1)) {
          ddd = ddd.concat(res);
          this.setData({
            content: ddd
          });
        }

        //console.log("len",res)
        //console.log("gf",this.data.content)
        asign += 1;
        bsign += 1;
        this.getdata(alength, data, res);
        common.hideLoading();
      })
    }
  },
  //分页效果
  bindDownLoad: function () {
    if (this.data.lenpage > this.data.num) {
      var di = this.data.num;
      var di = di + 1;
      this.setData({
        num: di
      });
      this.misTopic();
    } else {
      common.showToast("到底了", "loading");
    }
  },
  scroll: function (event) {
    //该方法绑定了页面滚动时的事件，我这里记录了当前的position.y的值,为了请求数据之后把页面定位到这里来。

  },
  topLoad: function (event) {
    //  该方法绑定了页面滑动到顶部的事件，然后做上拉刷新
  },
  answerRecord: function (event) {
    wx.navigateTo({
      url: "../answerRecord/answerRecord?id=" + event.currentTarget.dataset.id
    });
  },
  ReductTopic: function (event) {
    //跳转
    wx.navigateTo({
      url: "../testpra/testpra?title=订正&&iscorrect=1&&subscribing=1&&remarktype=5&&examtype=1&&testid=" + event.currentTarget.dataset.testid
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    asign = 0;
    bsign = 0;
    bbb = [];
    aaa = [];
    ccc = [];
    ddd = [];
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

})